import { _decorator, Node } from "cc";
import { ecs } from "db://oops-framework/libs/ecs/ECS";
import { CCComp } from "db://oops-framework/module/common/CCComp";
import { UIID } from "../../common/config/GameUIConfig"
import { oops } from "db://oops-framework/core/Oops";
import { Sprite } from "cc";
import { Label } from "cc";
import { Button } from "cc";
import { stringify } from "querystring";
import { GameComponent } from "db://oops-framework/module/common/GameComponent";

const { ccclass, property } = _decorator;

/** 视图层对象 */
@ccclass('firstViewComp')
@ecs.register('firstView', false)
export class firstViewComp extends GameComponent {
    /** 视图层逻辑代码分离演示 */
    @property(Sprite)
    private yang: Sprite = null!;

    @property(Sprite)
    private lang: Sprite = null!;

    @property(Sprite)
    private cai: Sprite = null!;

    @property(Sprite)
    private ren: Sprite = null!;

    @property(Sprite)
    private chuan: Sprite = null!;

    @property(Sprite)
    private success: Sprite = null!;

    @property(Sprite)
    private fail: Sprite = null!;


    @property(Button)
    private btn_chuan: Button = null!;

    loadedList: string[] = [];// 记录上船精灵 默认农夫在船上 不做记录

    acrossList: string[] = []; // 记录对岸精灵 就是已过河 不包括农夫  

    start() {
        // const entity = this.ent as ecs.Entity;         // ecs.Entity 可转为当前模块的具体实体对象

        this.yang.node.setScale(-1, 1);

        this.ren.node.on(Node.EventType.TOUCH_START, this.dragEvent, this);
        this.yang.node.on(Node.EventType.TOUCH_START, this.dragEvent, this);
        this.cai.node.on(Node.EventType.TOUCH_START, this.dragEvent, this);
        this.lang.node.on(Node.EventType.TOUCH_START, this.dragEvent, this);

    }

    dragEvent(event: Event) {
        const clickedNode = event.currentTarget as unknown as Node;

        switch (clickedNode.name) {
            case '羊':
                if (this.loadedList.indexOf(clickedNode.name) == -1) {
                    if (this.loadedList.length == 0) {
                        this.loadedList.push(clickedNode.name);
                        this.yang.node.setPosition(-74, -155, 0);
                    } else {
                        oops.gui.toast("小船已经满员啦~")
                    }
                } else {
                    this.yang.node.setPosition(-93, -5, 0);
                    this.loadedList = [];
                }
                break;
            case '狼':
                if (this.loadedList.indexOf(clickedNode.name) == -1) {
                    if (this.loadedList.length == 0) {
                        this.lang.node.setPosition(-74, -155, 0);
                        this.loadedList.push(clickedNode.name);
                    } else {
                        oops.gui.toast("小船已经满员啦~")
                    }

                } else {
                    this.lang.node.setPosition(-104, 58, 0);
                    this.loadedList = [];
                }
                break;
            case '菜':
                if (this.loadedList.indexOf(clickedNode.name) == -1) {
                    if (this.loadedList.length == 0) {
                        this.cai.node.setPosition(-74, -155, 0);
                        this.loadedList.push(clickedNode.name);
                    } else {
                        oops.gui.toast("小船已经满员啦~")
                    }

                } else {
                    this.cai.node.setPosition(-164, -8, 0);
                    this.loadedList = [];
                }
                break;
            default:
                break;
        }
    }

    //上船
    handleGoOnBoard() {
        this.chuan.node.active = !this.chuan.node.active;

        // 获取【出发】下的label 用于修改文字内容
        const label = this.btn_chuan.node.getChildByName('Label')?.getComponent(Label);

        if (this.chuan.node.active) {
            label!.string = "出发";
            this.ren.node.setPosition(-84, -220, 0);
        } else {
            // 这一步送货到对岸
            if (this.loadedList.length != 0) {
                // 满足条件  送货 
                switch (this.loadedList[0]) {
                    case "羊":
                        this.yang.node.setPosition(93, -5, 0);
                        this.acrossList.push('羊');
                        break;
                    case "狼":
                        this.lang.node.setPosition(104, 58, 0);
                        this.acrossList.push("狼");
                        break;
                    case "菜":
                        this.cai.node.setPosition(164, -8, 0);
                        this.acrossList.push("菜");
                        break;
                    default:
                        break;
                }

                console.log("已过河=>>> ", this.acrossList);


                if (this.methodCrossTheRiver(['羊', '狼', '菜']) && this.acrossList.length >= 3) {
                    // 通关 ✅ 游戏已完成
                    console.log("通关！！");
                    this.success.node.active = true;
                    return;
                }
                if (this.methodCrossTheRiver(['羊', '菜']) && this.acrossList.length == 2) {
                    // 失败
                    console.log("失败！ 羊 把 菜 吃了");
                    this.fail.node.active = true;
                    return;
                }
                if (this.methodCrossTheRiver(['羊', '狼']) && this.acrossList.length == 2) {
                    // 失败
                    console.log("失败！ 羊 被 狼 吃了");
                    this.fail.node.active = true;
                    return;
                }

                label!.string = "上船";
                this.ren.node.setPosition(-176, 69, 0);
                this.loadedList = []

            } else {
                // 不满足条件
                oops.gui.toast("小船还未装满哦~")
                this.chuan.node.active = true;
            }

        }
    }
    /** 事件 */
    // 返回首页
    handleBackHome() {
        oops.gui.open(UIID.Demo);
        oops.gui.remove(UIID.First);
    }
    // 下一关
    handleNext(){
        oops.gui.toast("下一关")
        oops.gui.open(UIID.Second);
        oops.gui.remove(UIID.First);
    }
    /** 方法 */
    // 检验是否在对岸或已过河的数组中 
    methodCrossTheRiver(list: string[]) {
        var result = false;
        var steps = list.length;
        for (let index = 0; index < list.length; index++) {
            const element = list[index];
            result = this.acrossList.includes(element);
            steps = result ? steps-1: steps;
        }
        console.log("result:", result, list, this.acrossList,steps);

        return result && steps == 0;
    }
    /** 视图对象通过 ecs.Entity.remove(firstViewComp) 删除组件是触发组件处理自定义释放逻辑 */
    reset() {
        this.node.destroy();
    }
}